//
//  BaseBackViewController.m
//  share_business
//
//  Created by baidufz on 2017/8/27.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "BaseBackViewController.h"
#import "UIButton+ImageTitleSpacing.h"
#import "UIColor+Extension.h"
#import "BSConfig.h"
#import "UILabel+Extension.h"
#import "UIView+Extension.h"
#import "MineViewController.h"
#import "MessageViewController.h"

@interface BaseBackViewController ()
@property (nonatomic, assign) BOOL defaultMessageButton;
@end

const static int btnWidth = 38 ;

@implementation BaseBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)viewDidAppear:(BOOL)animated {
    [self setDefaultBackButton];
    [self hideTabBar];

}

- (void)setDefaultBackButton {
    UIImageView *backView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back"]];
    [backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(defaultButtonAction)]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
}

- (void)defaultButtonAction {
    UINavigationController *nav = self.navigationController;
//    if (nav.viewControllers.count == 1) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
    [nav popViewControllerAnimated:YES];
}

- (void)defaultMessageRightButton {
    _defaultMessageButton = YES;
    [self topRightItemButton:[UIImage imageNamed:@"home_message"] title:@"消息"];
}

//顶部右边按钮
-(void)topRightItemButton:(UIImage *)image title:(NSString *)title{
    //修改方法
    UIView *rightBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0, btnWidth, btnWidth)];
    //文字大小
    NSInteger textFont = 12 ;
    //文字颜色
    UIColor *textColor = [UIColor colorWithHexString:COLOR_DARK_GRAY];
    //按钮大小
    CGSize btnSize =  CGSizeMake(btnWidth, btnWidth) ;
    if (image) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image forState:UIControlStateNormal];
        button.titleLabel.systemFont = textFont ;
        button.size = btnSize;
        button.tag = 1001 ;
        if (title) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:textColor forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
        }
        [rightBtnView addSubview:button];
        button.x = 8 ;
        [button addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    }else if(title){
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.systemFont = 14 ;
        button.size = btnSize;
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:textColor forState:UIControlStateNormal];
        [rightBtnView addSubview:button];
        button.x = 8 ;
        button.tag = 1001 ;
        [button addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtnView];
    self.navigationItem.rightBarButtonItem= rightItem;
}

- (void)rightAction:(UIButton *)button {
    if (_defaultMessageButton) {
        MessageViewController *ctrl = [[MessageViewController alloc] init];
        [self.navigationController pushViewController:ctrl animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
